<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>用户列表--CRM客户关系管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="resources/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input searchVal" placeholder="请输入搜索用户姓名" />
                </div>
                <a class="layui-btn search_btn" data-type="reload">搜索</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-normal addUsers_btn">添加用户</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
            </div>
        </form>
    </blockquote>
    <table id="userList" lay-filter="userList"></table>
    <!--操作-->
    <script type="text/html" id="userListBar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="look">查看</a>
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="assignRole">分配角色</a>
    </script>
</form>
<script type="text/javascript" src="resources/layui/layui.js"></script>
<script type="text/javascript" th:inline="none">
    layui.use(['form','layer','table','laytpl'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table;
        //用户列表
        var tableIns = table.render({
            elem: '#userList',
            url : 'users.do',
            cellMinWidth : 95,
            page : true,
            height : "full-125",
            limits : [10,15,20,25],
            limit : 20,
            id : "userListTable",
            cols : [[
                {type: "checkbox", fixed:"left", width:50},
                {field: 'telephone', title: '电话号码', minWidth:100, align:"center"},
                {field: 'realname', title: '姓名', align:'center'},
                {field: 'birthday', title: '生日', align:'center', minWidth:110, templet:function(obj){
                        return obj.birthday;
                }},
                {field: 'dept', title: '所属部门', align:'center',minWidth:150,templet:function (obj) {
                        return obj.dept.dname;
                 }},
                {field: 'available', title: '用户状态',  align:'center',templet:function(obj){
                        return obj.available == 1 ? "正常使用" : "限制使用";
                    }},
                {title: '操作', minWidth:175, templet:'#userListBar',fixed:"right",align:"center"}
            ]]
        });
        //实现搜索功能
        $(".search_btn").click(function () {
            var name=$(".searchVal").val();
            table.reload('userListTable',{url:"users.do?realname="+name});
        });
        $(".addUsers_btn").click(function(){
            addUsers();
        })
        //添加用户
        function addUsers(edit){
            var index = layui.layer.open({
                title : "添加用户",
                type : 2,
                content : "toUserPage.do",
                success : function(layero, index){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500)
                }
            })
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            })
        }
        // tool(xx)这里的xx是和上边表个的lay-filter="userList"对应的
        table.on("tool(userList)",function (obj) {
            //获取当前行的数据
            var data=obj.data;
            switch (obj.event){
                case 'del':
                    deleteUser(data);
                    break;
                case 'assignRole':
                    selectRole(data);
                    break;
            }
        });
        //删除方法
        function deleteUser(data){
            layer.confirm('您确定要删除【' + data.telephone + '】这个用户吗？',{icon: 3, title: '提示'},function (index) {
                $.ajax({
                    url:"user/deleteuser.do",
                    data:{id:data.id},
                    type:"get",
                    dataType:"json",
                    success:function (res) {
                        console.log(res+"****")
                        if(res.code==200){
                            //刷新表格
                            tableIns.reload();
                        }else if(res.code==-1){
                            layer.msg(res.msg);
                        }
                    }

                });
                //关闭提示框
                layer.close(index);
            });
        }
        //批量删除
        $(".delAll_btn").click(function () {
            var checkStatus = table.checkStatus('userListTable');
            var data = checkStatus.data;
            var uIds = [];
            if(data.length > 0) {
                for (var i in data) {
                    uIds.push(data[i].id);
                }
                layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                    $.get("/user/deleteBatchUser.do",{ids:uIds},function (res) {
                        tableIns.reload();
                        layer.close(index);
                    });
                })
            }else{
                layer.msg("请选择需要删除的用户");
            }
        });
        //开发分配角色的弹出框
        function selectRole(data) {
            var assignRoleIndex = layer.open({
                type: 2,
                title: "分配角色",
                content: "/toAssignRole.do",
                area: ['650px', '350px'],

                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);//少了这个是不能从父页面向子页面传值的
                    body.contents().find("#userIdHiden").val(data.id);//这样就将值传递给子页面中id为userIdHiden的隐藏域
                }
            });
            window.sessionStorage.setItem("assignRoleIndex", assignRoleIndex);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(window.sessionStorage.getItem("index"));
            })
        }

    });
</script>
</body>
</html>